package tr.edu.yildiz.ce.dap.appagent.process;

import java.io.IOException;

import tr.edu.yildiz.ce.dap.appagent.manager.AgentProcessManager;
import tr.edu.yildiz.ce.dap.appagent.manager.PromptManager;
import tr.edu.yildiz.ce.dap.appagent.process.base.AbstractProcess;

public class PromptProcess extends AbstractProcess implements Runnable{

	public PromptProcess(AgentProcessManager processManager) {
		super(processManager);
	}

	@Override
	public void run() {
		while (true) {
			try {
				byte[] bytes = new byte[512];
				System.out.print("app-agent command : ");
				System.in.read(bytes, 0, bytes.length);
				String commandStr = new String(bytes);
				commandStr = cleanCommand(commandStr); 
				PromptManager.callPromptCommand(this, commandStr);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	private String cleanCommand(String commandStr){
		commandStr = commandStr.substring(commandStr.indexOf('-')+ 1);
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i < commandStr.length(); i++) {
			char c = commandStr.charAt(i);
			if ( c != 13)
				sBuilder.append(c);
			else 
				break;
		}
		return sBuilder.toString();
	}
	
	
}
